#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"

void QNodeInit(point* pst)
{
	pst->head = NULL;
	pst->tail = NULL;
	pst->size = 0;
}

void QNodeDestroy(point* pq)
{
	QNode* pcur = pq->head;
	while (pcur)
	{
		QNode* presever = pcur->next;
		free(pcur);
		pcur = NULL;
		pcur = presever;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QNodepush(point* pq, Qdatatype x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(1);
	}
	newnode->valu = x;
	newnode->next = NULL;
	if (pq->head==NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

void QNodepop(point* pq)
{
	assert(pq);
	assert(pq->size);
	if ( pq->head->next==NULL)
	{
		free(pq->tail);
		pq->tail=pq->head = NULL;
		pq->size--;
	}
	else
	{
	    QNode* presever = pq->head->next;
		free(pq->head);
		pq->head = NULL;
		pq->head = presever;
		pq->size--;
	}
	
}

Qdatatype QNodefront(point* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->valu;
}

Qdatatype QNodeback(point* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->valu;
}

int QNodeSize(point* pq)
{
	assert(pq);
	return pq->size;
}

bool QNodeEmpty(point* pq)
{
	assert(pq);
	return pq->size == 0;
}